ZoneWindow = function() {
	this.zone = new Ext.form.TextField({
		fieldLabel: 'Enter <b>zone</b> to add',
		width: 450,
		validationEvent: false,
		allowBlank: false,
		id: 'zoneInput',
		blankText: 'Zone is required.',
		msgTarget: 'under',
		// without these listeners the window will look funny after an error message
		listeners: {
			valid: this.syncShadow,
			invalid: this.syncShadow,
			scope: this
		}
	});
	
	this.form = new Ext.FormPanel({
		labelAlign:'top',
		items: this.zone,
		border: false,
		bodyStyle:'background:transparent;padding:10px;'
	});
	
	ZoneWindow.superclass.constructor.call(this, {
		title:'Add Zone',
		iconCls:'add-zone-icon',
		id:'zoneWindow',
		autoHeight:true,
		width:500,
		resizable:false,
		plain:true,
		modal: false,
		y: 100,
		autoScroll: true,
		closeAction: 'hide',
		buttons:[{
			text: 'Add Zone',
			handler: this.onAdd,
			scope: this
		},{
			text: 'Cancel',
			handler: this.hide.createDelegate(this, []) // hide
		}],
		
		items: this.form,
		focus: function(){
			this.zone.focus(); // set focus
		}
	});
}

Ext.extend(ZoneWindow, Ext.Window, {
	show : function(){
		if(this.rendered){
			this.zone.setValue(''); // reset text field
			this.zone.clearInvalid(); // clears the default empty warning
		}
		ZoneWindow.superclass.show.apply(this, arguments);
	},
	onAdd: function() {
		this.el.mask('Processing...', 'x-mask-loading');
		var zone = this.zone.getValue();
		Ext.Ajax.request({
			url: 'xhr/zone/add.php',
			params: {zone: zone},
			success: function(response, options){
				this.zone.clearInvalid(); // clear error messages
				
				var jsonData = new Object;
				try {
					jsonData = Ext.util.JSON.decode(response.responseText);
				}
				catch (err) {
					jsonData.success = false;
					jsonData.msg = "Error decoding JSON response!";
				}
				
				if (jsonData.success) {
					this.addZone(zone);
					this.el.unmask();
					this.hide();
				} else {
					this.zone.markInvalid(jsonData.msg);
					this.el.unmask();
				}
				Ext.ux.Status.log({
					msg: jsonData.msg,
					success: jsonData.success
				});
			},
			failure: function(response, options){
				this.zone.markInvalid('Cannot add zone. XHR failure!');
				this.el.unmask();
				// log to status panel
				Ext.ux.Status.log({
					msg: "Cannot add zone. XHR failure!",
					type: 'error'
				});
			},
			scope: this,
		});
	},
	addZone: function(zone) {
		console.debug('add zone');
		console.debug(zone);
		Ext.getCmp('zonePanel').addZone({
			zone: zone
		});
	}
});